package com.brillilab.starter.controller.aio.method;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.method.MethodDealStateEnum;
import com.brillilab.domain.vo.method.MethodShareDeal;
import com.brillilab.domain.vo.method.MethodShareListItem;
import com.brillilab.domain.vo.method.MethodShareTo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.method.MethodShareLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author wuzhiyu
 * @Title: MethodShareController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/2/2313:40
 */
@RestController
@RequestMapping("/api/methodShare")
public class MethodShareController {
    @Resource
    private UsersLogic usersLogic;
    @Resource
    private MethodShareLogic methodShareLogic;

    @AspectLog
    @PostMapping("/share")
    public ResponseVo share(@RequestBody MethodShareTo methodShareTo, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.notNull(methodShareTo, "参数缺失");
        Assert.notNull(methodShareTo.getReceiverId(), "参数缺失");
        Assert.isTrue(!CollectionUtils.isEmpty(methodShareTo.getMethodIdList()), "参数缺失");
        UserInfoVo user = usersLogic.getUserByToken(token);
        return ResponseVo.success(methodShareLogic.share(methodShareTo, user));
    }

    @GetMapping("/shareToMeDetail")
    public ResponseVo shareToMeDetail(Long shareId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.notNull(shareId, "参数缺失");
        UserInfoVo user = usersLogic.getUserByToken(token);
        return ResponseVo.success(methodShareLogic.getShareInfo(shareId, user));
    }

    @AspectLog
    @PostMapping("/shareToMeDeal")
    public ResponseVo shareToMeDeal(@RequestBody MethodShareDeal deal, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.notNull(deal, "参数缺失");
        Assert.notNull(deal.getShareId(), "参数缺失");
        if (!deal.getState().equals(MethodDealStateEnum.Refuse.getState())) {
            Assert.notNull(deal.getExist(), "参数缺失");
        }
        UserInfoVo user = usersLogic.getUserByToken(token);
        return ResponseVo.success(methodShareLogic.deal(deal, user));
    }

    @GetMapping("/shareFromList")
    public ResponseVo shareFromList(Integer state, Integer pageIndex, Integer rows, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        pageIndex = pageIndex == null ? 1 : pageIndex;
        rows = rows == null ? 10 : rows;
        UserInfoVo user = usersLogic.getUserByToken(token);
        return ResponseVo.success(methodShareLogic.listShareToMe(user, state, pageIndex, rows));
    }

    @GetMapping("/shareList")
    public ResponseVo shareList(Integer pageIndex, Integer rows, boolean isDesc, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        pageIndex = pageIndex == null ? 1 : pageIndex;
        rows = rows == null ? 10 : rows;
        UserInfoVo user = usersLogic.getUserByToken(token);
        return ResponseVo.success(methodShareLogic.listMyShare(user, pageIndex, rows, isDesc));
    }
}
